package org.dromara.application.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.application.domain.SysSystemInfoConfig;
import org.dromara.application.domain.bo.SysSystemInfoConfigBo;
import org.dromara.application.domain.vo.SysSystemInfoConfigVo;
import org.dromara.application.mapper.SysSystemInfoConfigMapper;
import org.dromara.application.service.SysSystemInfoConfigService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统系信息Service业务层处理
 *
 * @author liushuang
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@Service
public class SysSystemInfoConfigServiceImpl implements SysSystemInfoConfigService {

    private final SysSystemInfoConfigMapper baseMapper;
    private final OssService ossService;

    /**
     * 查询系统系信息
     *
     * @param id 主键
     * @return 系统系信息
     */
    @Override
    public SysSystemInfoConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询系统系信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 系统系信息分页列表
     */
    @Override
    public TableDataInfo<SysSystemInfoConfigVo> queryPageList(SysSystemInfoConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSystemInfoConfig> lqw = buildQueryWrapper(bo);
        Page<SysSystemInfoConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的系统系信息列表
     *
     * @param bo 查询条件
     * @return 系统系信息列表
     */
    @Override
    public List<SysSystemInfoConfigVo> queryList(SysSystemInfoConfigBo bo) {
        LambdaQueryWrapper<SysSystemInfoConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSystemInfoConfig> buildQueryWrapper(SysSystemInfoConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSystemInfoConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSystemName()), SysSystemInfoConfig::getSystemName, bo.getSystemName());
        lqw.like(StringUtils.isNotBlank(bo.getTechnicalSupport()), SysSystemInfoConfig::getTechnicalSupport, bo.getTechnicalSupport());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), SysSystemInfoConfig::getLogo, bo.getLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getBgi()), SysSystemInfoConfig::getBgi, bo.getBgi());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysSystemInfoConfig::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增系统系信息
     *
     * @param bo 系统系信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysSystemInfoConfigBo bo) {
        SysSystemInfoConfig add = MapstructUtils.convert(bo, SysSystemInfoConfig.class);
        validEntityBeforeSave(add);
        add.setLogoUrl(ossService.selectUrlByIds(add.getLogo()) == null ? "" : ossService.selectUrlByIds(add.getLogo()));
        add.setBgiUrl(ossService.selectUrlByIds(add.getBgi()) == null ? "" : ossService.selectUrlByIds(add.getBgi()));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统系信息
     *
     * @param bo 系统系信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysSystemInfoConfigBo bo) {
        SysSystemInfoConfig update = MapstructUtils.convert(bo, SysSystemInfoConfig.class);
        validEntityBeforeSave(update);
        if(ObjectUtil.isNotNull(update.getLogo())){
            update.setLogoUrl(ossService.selectUrlByIds(update.getLogo()) == null ? "" : ossService.selectUrlByIds(update.getLogo()));
        }
        if(ObjectUtil.isNotNull(update.getBgi())){
            update.setBgiUrl(ossService.selectUrlByIds(update.getBgi()) == null ? "" : ossService.selectUrlByIds(update.getBgi()));
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSystemInfoConfig entity){
        //TODO 做一些数据校验,如唯一约束
        //保证只有一条status=0的数据
        boolean exists = baseMapper.exists(new LambdaQueryWrapper<SysSystemInfoConfig>()
            .eq(SysSystemInfoConfig::getStatus, "0")
            .ne(ObjectUtil.isNotNull(entity.getId()), SysSystemInfoConfig::getId, entity.getId()));
        if (exists){
            throw new ServiceException("只能存在一条有效数据,请先停用!");
        }
    }

    /**
     * 校验并批量删除系统系信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Map<String, Object> getSystemIofo() {
        Map<String, Object> result = new HashMap<>();
        SysSystemInfoConfig sysSystemInfoConfig = baseMapper.selectOne( new LambdaQueryWrapper<SysSystemInfoConfig>());
        if(sysSystemInfoConfig != null){
            result.put("systemName",sysSystemInfoConfig.getSystemName());
            result.put("technicalSupport",sysSystemInfoConfig.getTechnicalSupport());
            result.put("logoUrl",sysSystemInfoConfig.getLogoUrl());
            result.put("bgiUrl",sysSystemInfoConfig.getBgiUrl());
            result.put("internetLabTitle",sysSystemInfoConfig.getInternetLabTitle());
        }
        return result;
    }
}
